package com.example.myblog.config;

import com.example.myblog.common.ApplicationVariable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//将控制权交给Spring
@Component
// 实现拦截器
public class loginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户登录，加false时说明当没有session时，不要创建session
        HttpSession session=request.getSession(false);
        if (session != null&&session.getAttribute(ApplicationVariable.SESSION_USERINFO_KEY )!=null) {
            return true;
        }
        //执行这个说明用户未登录
        response.sendRedirect("login.html");
        return false;
    }
}
